<?php
include_once "../init.inc.php";
if ($log->admincheck($_SESSION['loggedin'], "logon", "password", "useremail", $_SESSION['loggedusr']) == false){
    die(include("retry.php"));
    };
include_once '../inc/header.php';
include_once "../class/pageNavigator.php";

  define("PERPAGE", 25);
  define("OFFSET", "offset");
  $otherparameter="";
$cat_name = @strip_tags($_REQUEST['cat_name']);

?>
		




<?php
//****************************************************************************************
if ($_GET['cat']){
    
    // тестовый вариант
    if (!Am_Lite::getInstance()->isUserActive()){
      //  echo "<h4 class=\"alert\">Пользователь без активной подписки</h4>";
    }  
  
$offset=@$_GET['offset'];
  
        if (!isset($offset)){
                $recordoffset=0;
        }else{
                //calc record offset
                $recordoffset=$offset*PERPAGE;
        }

 //убираем полнотекстовый поиск
//$type = $_GET['type'];
$order = mysql_real_escape_string($_REQUEST['order']);
$intitle = (int)$_REQUEST['checktitle'];
$type = 'or';
$cat = (int)$_GET['cat'];
/*------------------------------------------*/
$otherparameter2="";
$otherparameter2.= $_REQUEST['articles']?"&amp;articles=1":"";
$otherparameter2.= $_REQUEST['ebook']?"&amp;ebook=1":"";
$otherparameter2.= $_REQUEST['video']?"&amp;video=1":"";
$otherparameter2.= $_REQUEST['plr']?"&amp;plr=1":"";
$otherparameter2.= $_REQUEST['mrr']?"&amp;mrr=1":"";
$otherparameter2.= $_REQUEST['rr']?"&amp;rr=1":"";
$otherparameter2.= $_REQUEST['sales']?"&amp;sales=1":"";

/*-------------------------------------------*/

?>

    
 <?php  

 $sqlhead = "SELECT p.ID, p.post_content, p.post_title, p.prem, p.words, c.cat_name, p.post_date ";
 
 $sql = "FROM `posts` as p 
                        LEFT JOIN `relations` AS r ON p.ID = r.post_id
                        LEFT JOIN `category` AS c ON r.cat_id = c.ID
                        WHERE 1=1 ";

    
    
//********************************************************************************************
    
    if ($cat>0){
        $sql .=" AND c.ID='$cat'";
    }
    
        $totalrecords = getCol($sql);
        $sql_bezlimit = $sqlhead.$sql;
        getAllId($sql_bezlimit);
        
    if ($order){
        $sql .= " ORDER BY p.".$order;
    }
        // для пагинации
        $sql .= " LIMIT $recordoffset, ".(int)PERPAGE;
        
        
    
 $sql = $sqlhead.$sql;
    
    $result = mysql_query($sql) or die('Error, insert query failed');
        //$totalrecords = mysql_num_rows($result); // Узнаем количество элементов в выборке
    
    $otherparameter = "&amp;cat=$cat&amp;type=$type&amp;search=$search";
  
?>
      <h3  class="text-info">Niche edit &rarr;<spans style="color:black;"><?php echo getCat($cat);?></span></h3>
   
    
  <h4><?php echo $totalrecords; ?><span class="text-info">&nbsp;results found&nbsp;</span>
      
      <?php if ($totalrecords>0): ?>
        
  </h4>
      <?php endif; ?>
      

  
    <form action="" method="get">
           <table class="main_table">
               <thead>
               <tr>  
               <th><i class="icon-filter"></i>&nbsp;<a href="<?php echo "?order=post_title".$otherparameter.$otherparameter2 ?>">Title</a></th>
               <th><i class="icon-filter"></i>&nbsp;<a href="<?php echo "?order=words".$otherparameter.$otherparameter2 ?>"">Words</a></th>
               <th><i class="icon-filter"></i>&nbsp;<a href="<?php echo "?order=post_date".$otherparameter.$otherparameter2 ?>">Date</a></th>
                           
               </tr>
               </thead>
           
                <?php    while ($row = mysql_fetch_assoc($result)) { ?>
                             <tr>
                                        
                    <?php    foreach ($row as $key=>$cellValue){ 
                                        if ($key=="ID" or $key=="prem" or $key=="cat_name"  or $key=="post_content") continue;
                                        if ($key=="post_date") {
                                            //выберем дату
                                            $data = explode(" ", $cellValue);
                                            echo "<td>".$data[0]."</td>";
                                            continue;
                                        }
                                        if ($key=="post_title")  {?>
                                        <td class="left-align">
                                           <a href="<?php echo "admin_edit_post.php?id=".$row['ID']."&amp;cat=$cat&amp;type=$type&amp;search=$search";?>"> 
                                              <?php echo excerpt($cellValue); ?>
                                           </a>
                                        </td>
                                         <?php }else { ?>
                                             <td>
                                           
                                              <?php echo excerpt($cellValue); ?>
                                          
                                             </td>
                                             
                                             
                                       <?php       
                                         }
                                                             
                                                                } 
                                        
                                       
                                      ?></tr><?php
                   }   ?>
                                    
           </table>
    </form>
    
<?php  

//**************************************************************************************
              
                
$pagename=basename($_SERVER['PHP_SELF']);

$numpages = ceil($totalrecords/PERPAGE);
//create category parameter
$otherparameter = "&amp;cat=$cat&amp;type=$type&amp;search=$search&amp;order=$order".$otherparameter2;
//create if needed


  if($numpages>1){
            /*create navigator
            $nav = new pageNavigator($pagename, $totalrecords, PERPAGE, 
                    $recordoffset, 4, $otherparameter);
             
             */
            $nav = pageNavigator::getInstance($pagename, $totalrecords, PERPAGE, $recordoffset, 5, $otherparameter);
            
            echo $nav->getNavigator();
            
                }
                

//***************************************************************************************


}

//********************************Добавить все в корзину*********************************
if (@$_GET['0']){
        $id = array();
        foreach ($_GET as $key=>$value ){
              $id[] .=(int)$value;
        }

        $cookieid = $_COOKIE['basket'];
        $cookieid  = explode(",", $cookieid);
        $out = array();
        $out = array_merge($id, $cookieid);
        $out = array_unique($out);


        $out = implode("," , $out );
        //print_r($out);
        
        echo '<script>$.cookie("basket","'.$out.'", {path: "/"});
            history.back();
            </script>';
        //$.cookie("basket", basketRet, {path: "/"});
        //setcookie("basket", $out, 0, "/");


}

function getCol($sql){
    
  $result =  mysql_query("SELECT count(*) as count ".$sql);
    
  $rows = mysql_fetch_assoc($result);
  
  return (int)$rows['count'];
    
    
}

function getAllId($sql){
    
   $result =  mysql_query($sql); 

   $allid = array();
   
        while ($rowid = mysql_fetch_assoc($result)) {
            $allid[] .= $rowid['ID'];
        }

    ?>
    <script>

  

 //********************add all to cart ***********************************
    function addall(){
       // var params = $(this).attr('href').split('?');
     //   alert("this.search");
        $.ajax({  
				type: "POST",  
				url: "downloadall.php",  
				data: { str: "<?php echo http_build_query($allid); ?>" },
				success: function(theResponse) {
                                       start();
                                }  
			}); 
        
     
        return false;
    };
 
 
    </script>
    
    
    <?php
        
}



function getCat($id){
  
    $cat = array();
    $sql = "SELECT cat_name FROM `category`
            WHERE category.ID='$id'";
    $result = mysql_query($sql); 
        $rows = mysql_fetch_assoc($result);
        return $rows['cat_name'];
 
}




?>
<?php  include_once 'footer.php'; ?>